package com.zq.solution.array;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName Intersection
 * @Description
 * @Author ZQ
 * @Date 2020/11/2 17:07
 * @Version V1.0
 **/
public class Intersection {
    public static int[] intersection(int[] nums1, int[] nums2) {

        Set<Integer> set1 = removeRepetition(nums1);
        Set<Integer> set2 = removeRepetition(nums2);

        return intersection(set1,set2);
    }
    public static int[] intersection(Set<Integer> set1,Set<Integer> set2){
        Set<Integer> set = new HashSet<>();
        if (set1.size() > set2.size()) return intersection(set2,set1);
        for (Integer num1 : set1){
            if (set2.contains(num1))
                set.add(num1);
        }
        Integer[] a =  set.toArray(new Integer[]{});
        int[] intArray = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            intArray[i] = a[i].intValue();
        }
        return intArray;
    }
    public static Set<Integer> removeRepetition(int[] a){
        Set<Integer> set = new HashSet<>();
        for (Integer num : a){
            set.add(num);
        }
        return set;
    }

    public static void main(String[] args) {
        int[] b = new int[]{1,2,1,1,1,4,5,6,7};
        int[] a = new int[]{1,2,1,1,1,6,7};
        for (Integer num : intersection(a,b)){
            System.out.print(num + ",");
        }
    }
}
